#!/bin/bash

config_clear() {
  local profile=$1

  if [ -z "$(os::username)" ]; then
    log::error "cannot get username"
    exit 1
  fi

  # # ZMICRO_BIN_PATH: /home/$(os::username)/.zmicro/bin
  # export ZMICRO_HOME=/home/$(os::username)/.zmicro
  # export ZMICRO_BIN_PATH=/home/$(os::username)/.zmicro/bin
  # export PATH=$ZMICRO_BIN_PATH:$PATH

  if [ ! -f "$profile" ]; then
    return
  fi

  # remove .zmicro version
  cat $profile | grep "# ZMICRO_BIN_PATH" >>/dev/null 2>&1
  if [ "$?" = "0" ]; then
    sed -i "/^# ZMICRO_BIN_PATH: \/home\/$(os::username)\/.zmicro\/bin$/d" $profile
    sed -i "/^export ZMICRO_HOME=\/home\/$(os::username)\/.zmicro$/d" $profile
    sed -i "/^export ZMICRO_BIN_PATH=\/home\/$(os::username)\/.zmicro\/bin$/d" $profile
    sed -i "/^export PATH=\$ZMICRO_BIN_PATH:\$PATH$/d" $profile
  fi
}

config_profile() {
  local profile=$1
  if [ ! -f $profile ]; then
    touch $profile
  fi

  cat $profile | grep "export ZMICRO_HOME=${ZMICRO_HOME}" >>/dev/null 2>&1
  if [ "$?" != "0" ]; then
    local text=$(
      cat <<-END
# ZMICRO
export ZMICRO_HOME=${ZMICRO_HOME}
export PATH=\$ZMICRO_HOME/bin:\$PATH 
END
    )
    echo "$text" | fs::write -a $profile
  fi
}

# Global, need root
config_global_rc() {
  local profile=$1

  # if not found, ignore
  if [ ! -f $profile ]; then
    log::error "[$(timestamp)][config_global_rc] profile $profile not found"
    exit 1
  fi

  cat $profile | grep "export ZMICRO_HOME=${ZMICRO_HOME}" >>/dev/null 2>&1
  if [ "$?" != "0" ]; then
    local text=$(
      cat <<-END
# ZMICRO
export ZMICRO_HOME=${ZMICRO_HOME}
export PATH=\$ZMICRO_HOME/bin:\$PATH 
END
    )
    echo "$text" | fs::write -a $profile
  fi
}

# config_global_rc_ignore_if_not_found() {
#   local profile=$1
#   if [ ! -f $profile ]; then
#     return
#   fi

#   config_global_rc $profile
# }

config_global_rc_force_create() {
  local profile=$1
  if [ ! -f $profile ]; then
    sudo touch $profile
  fi

  config_global_rc $profile
}

# common
config_common() {
  if [ -d "/etc/profile.d" ]; then
    config_global_rc_force_create /etc/profile.d/zmicro.sh
  fi
}

config_bashrc() {
  # OLD
  config_clear $HOME/.bashrc

  # HOME
  config_profile $HOME/.bashrc

  # Global
  if [ -f "/etc/bash.bashrc" ]; then
    config_global_rc /etc/bash.bashrc
  fi

  # # GLOBAL
  # #   ubuntu  20
  # config_global_rc_ignore_if_not_found /etc/bashrc
  # #   centos  8
  # config_global_rc_ignore_if_not_found /etc/bash.bashrc
}

config_zshrc() {
  # OLD
  config_clear $HOME/.zshrc

  # HOME
  config_profile $HOME/.zshrc

  # Global
  if [ -f "/etc/zsh/zshrc" ]; then
    config_global_rc /etc/zsh/zshrc
  fi

  # # GLOBAL
  # #   ubuntu 20
  # config_global_rc_ignore_if_not_found /etc/zsh/zprofile
  # #   centos 8
  # config_global_rc_ignore_if_not_found /etc/zshrc
}

# https://unix.stackexchange.com/questions/104727/add-a-path-in-path-globally-for-every-user
# if [ -f "/etc/environment" ]; then
#   config_profile /etc/environment
# else
#   config_profile /etc/profile
# fi

disable_oh_my_zsh_update() {
  local profile=$HOME/.zshrc

  # if not found, ignore
  if [ ! -f $profile ]; then
    log::error "[$(timestamp)][config_global_rc] profile $profile not found"
    exit 1
  fi

  cat $profile | grep "ZMICRO_DISABLE_AUTO_UPDATE" >>/dev/null 2>&1
  if [ "$?" != "0" ]; then
    local text=$(
      cat <<-END
      
# ZMICRO_DISABLE_AUTO_UPDATE
export DISABLE_AUTO_UPDATE="true"
END
    )
    echo "$text" | fs::write -a $profile
  fi
}

config_common
config_bashrc
config_zshrc

disable_oh_my_zsh_update
